<?php

namespace app\user\controller\metier\evaluation;

use app\admin\service\RatingScaleService;
use app\BaseController;
use app\model\MetierEvaluationResultModel;
use app\model\RatingScaleModel;
use app\model\RatingScaleQuestionModel;
use app\user\service\MetierService;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 列表
     * @return bool|string
     */
    public function list(): bool|string
    {
        try {
            $where[] = ['channel','=',5];
            $RatingScaleModel = new RatingScaleModel();
            $data = $RatingScaleModel->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 详情
     * @return bool|string
     */
    public function info(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $RatingScaleModel = new RatingScaleModel();
            $info = $RatingScaleModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info = $info->toArray();
            $RatingScaleQuestionModel = new RatingScaleQuestionModel();
            $info['question_num'] = $RatingScaleQuestionModel->where('rating_scale_id','=',$param['id'])->count('id');
            $RatingScaleService = new RatingScaleService($this->app);
            $question = $RatingScaleService->GetQuestion($param['id']);
            return success([
                'question' => $question,
                'info' => $info
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 提交结果
     * @return bool|string
     */
    public function getResult(): bool|string
    {
        $RatingScaleModel = new RatingScaleModel();
        $RatingScaleModel->startTrans();
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'rating_scale_id|量表ID' =>  'require',
                'finish_used_time|用户完成用时' =>  'require',
            ]);
            $MetierService = new MetierService($this->app);
            if (!$MetierService->generateReport([
                'company_id' => $this->userData['company_id'],//公司
                'areas_id' => $this->userData['areas_id'],//区域
                'unit_id' => $this->userData['unit_id'],
                'uid' => $this->userData['id'],//答题用户ID
                'rating_scale_id' => $param['rating_scale_id'],//量表ID
                'userAnswer' => $param['question_answer']??[],//用户填写的答案
                'finish_used_time' => $param['finish_used_time'],//用户完成用时
            ])) throw new \Exception('提交失败！'.$MetierService->message.$MetierService->line);
            $RatingScaleModel->commit();
            return success([
                'id' => $MetierService->ResultId
            ]);
        } catch (\Exception $e) {
            $RatingScaleModel->rollback();
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 结果详情
     * @return bool|string
     */
    public function resultInfo(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $MetierEvaluationResultModel = new MetierEvaluationResultModel();
            $info = $MetierEvaluationResultModel->with([
                'ratingScale' => function ($query) {
                    $query->with([
                        'ratingScaleTypeName',
                        'RatingScaleMode'
                    ]);
                },
                'ratingScaleFactorWarning' => function ($query) {
                    $query->with(['factorName']);
                },
                'factorScore' => function ($query) {
                    $query->with([
                        'ratingScaleFactor',
                        'ratingScaleFactorLevel'
                    ]);
                }
            ])->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}